<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article_model extends CI_Model {

	//获取分类
	public function getcate(){
		$this->db->order_by('id','DESC');
		return $this->db->get('cate');
	}

	//添加文章
	public function addart($post){
		if($post['description']==""){
			$post['description']=strip_tags(mb_substr($post['content'],0,200,'utf-8'));
		}
		$data = array(
			'title' =>	$post['title'],
			'cate' =>	$post['cate'],
			'text' =>	$post['content'],
			'uid' =>	$post['uid'],
			'description'=>$post['description'],
			'tag' =>	$post['tag']
		);

		return $this->db->insert('article',$data);
	}

	//获取所有文章
	public function getallart(){
		$this->db->order_by('id','DESC');
		$this->db->where('sta',1);
		return $this->db->get('article');
	}

	//获取正常状态的文章
	public function getrightart($num=10,$limt=0){
		$this->db->order_by('id','DESC');
		$this->db->where('sta',1);
		$this->db->limit($num, $limt);
		$this->db->select('id, title, description,cate,time');
		return $this->db->get('article');
	}

	//获取所有删除状态的文章
	public function getalldelart(){
		$this->db->order_by('id','DESC');
		$this->db->where('sta',0);
		return $this->db->get('article');
	}

	//根据ID获取文章
	public function getartbyid($id){
		return $this->db->get_where('article',array('id'=>$id));
	}

	//更新文章
	public function updateart($post){
		if($post['description']==""){
			$post['description']=strip_tags(mb_substr($post['content'],0,200,'utf-8'));
		}
		$data = array(
			'title' =>	$post['title'],
			'cate' =>	$post['cate'],
			'text' =>	$post['content'],
			'uid' =>	$post['uid'],
			'description'=>$post['description'],
			'tag' =>	$post['tag']
		);
		
		$this->db->where('id',$post['id']);
		return $this->db->update('article', $data);
	}

	//查找文章
	public function searchart($post){
		$data = array(
			'title' => $post['word'],	
			'uid' => $post['editor'],	
		);
		if($post['cate']!=0){
				$data['cate']=$post['cate'];
		}

		$this->db->like($data);
		$this->db->order_by('id','DESC');
		$this->db->where('sta',1);
		return $this->db->get('article');
	}

	//根据分类ID获取该分类文章数
	public function getnumforcate($id){
		$res = $this->db->get_where('article',array('cate'=>$id));
		return $res->num_rows();
	}

	//根据ID获取分类
	public function getcateformid($id){
		return $this->db->get_where('cate',array('id'=>$id));
	}

	//新增分类
	public function addcate($name){
		$data = array(
			'name' => $name
		);

		return $this->db->insert('cate', $data);
	}

	//重命名分类
	public function renamecate($id,$name){
		$this->db->where('id', $id);
		return $this->db->update('cate', array('name'=>$name));
	}

	//根据UID获取文章
	public function getuidart($uid){
		return $this->db->get_where('article',array('uid'=>$uid));
	}

	//根据分类ID获取文章
	public function getcateart($cid){
		return $this->db->get_where('article',array('cate'=>$cid));
	}

	//根据id删除文章
	public function deleteart($id){
		$this->db->where('id', $id);
		return $this->db->update('article', array('sta'=>0));
	}

	//根据id恢复文章
	public function revert($id){
		$this->db->where('id', $id);
		return $this->db->update('article', array('sta'=>1));
	}

	//彻底删除文章
	public function deletebyid(){
		
	}


}